﻿<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle"> </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        showPagination="auto"
        :columns="columns"
        :data="loadData"
        :alert="false"
        :rowKey="(record) => record.id"
        :rowSelection="{ type:'radio', selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :customRow="customRow"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('PolicyTask:add')">
          <a-button type="primary" v-if="hasPerm('PolicyTask:add')" icon="plus" @click="$refs.addForm.add(policyId)"
            >新增策略任务</a-button
          >
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('PolicyTask:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('PolicyTask:edit') & hasPerm('PolicyTask:delete')" />
          <a-popconfirm
            v-if="hasPerm('PolicyTask:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => PolicyTaskDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import { PolicyTaskPage, PolicyTaskDelete } from '@/api/modular/main/PolicyTaskManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
export default {
  components: {
    STable,
    addForm,
    editForm,
  },
  props:['policyId','curTaskId'],
  data() {
    return {
      queryParam: {},
      columns: [
        {
          title: '标题',
          align: 'center',
          dataIndex: 'title',
        },
        {
          title: 'Cron表达式',
          align: 'center',
          dataIndex: 'crontabExpression',
        },
        {
          title: '摘要说明',
          align: 'center',
          dataIndex: 'description',
        },
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        this.queryParam.policyId=this.policyId
        return PolicyTaskPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  created() {
    if (this.hasPerm('PolicyTask:edit') || this.hasPerm('PolicyTask:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        align: 'center',
        fixed: 'right',
        scopedSlots: { customRender: 'action' },
      })
    }
  },
  methods: {
    /**
     * 查询参数组装
     */
    switchingDate() {
      const obj = JSON.parse(JSON.stringify(this.queryParam))
      return obj
    },
    PolicyTaskDelete(record) {
      PolicyTaskDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    //自定义选中当前行的操作
    customRow(record,index){
      return {
        on:{
          click:()=>{
            this.updateSelectedTask(record)
            this.onSelectChange([record.id],record)
          }
        }
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    updateSelectedTask(record){
      this.curTaskId=record.id
      this.$emit('selected', this.curTaskId)
    },
  },
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
